#!/bin/bash

jenkins_port=8888
soft_dir=/opt/softs
install_dir=/opt/jenkins
export JENKINS_HOME=${install_dir}/data
pid=`ps -ef | grep jenkins.war | grep -v grep | awk '{print $2}'`

function start(){
    cd ${soft_dir}
    nohup java -jar -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8 -Dhudson.security.csrf.GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION=true ${soft_dir}/jenkins.war --webroot=${install_dir}/war --httpPort=${jenkins_port} > ${install_dir}/logs/jenkins.log 2>&1 &
    sleep 3
    pid_recheck=`ps -ef | grep jenkins.war | grep -v grep | awk '{print $2}'`

    if [ -z ${pid_recheck} ];then
        echo -e "\033[31m started jenkins fail!  log:${install_dir}/logs/jenkins.log\033[0m"
    else
        echo -e "\033[32m started jenkins success! ${install_dir}/logs/jenkins.log\033[0m"
        echo -e "\033[32m pid ${pid_recheck} ...\033[0m"
    fi
}


# 启动
if [ -z ${pid} ];then
    echo -e "\033[31m jenkins starting..\033[0m"
    start
else
    echo -e "\033[31m restarting jenkins...\033[0m"
    kill -9 $pid
    sleep 3
    start
fi